﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;

namespace Mind.Data
{
    /// <summary>
    /// Class that wrapp a DbParameter. The type of the parameter is get with typeof(T).
    /// If T is object, the type is get with GetType() from the Value property.
    /// If Value is null, DbType.String will be choosen.
    /// </summary>
    /// <typeparam name="T">The param type</typeparam>
    public class DataAccessParameter<T> : IDataAccessParameter
    {
        public string Name { get; set; }
        public T Value { get; set; }
        public DbType? Type { get; set; }

        public DataAccessParameter(string name, T value, DbType type)
        {
            this.Name = name;
            this.Value = value;
            this.Type = type;
        }

        public DataAccessParameter(string name, T value)
        {
            this.Name = name;
            this.Value = value;
        }

        public DataAccessParameter(T value)
            : this(String.Empty, value)
        {

        }

        public DataAccessParameter()
            : this(String.Empty, default(T))
        {
        }


        public DbParameter ToDbParameter()
        {
            return ToDbParameter(DataAccessManager.Current);
        }

        public DbParameter ToDbParameter(DataAccess dataAcces)
        {
            DbParameter result;

            if (Type.HasValue)
            {
                result = dataAcces.CreateParameter(this.Name, this.Value, Type.Value);
            }
            else
            {
                if (typeof(T) == typeof(object))
                    result = dataAcces.CreateParameter(this.Name, this.Value);
                else
                    result = dataAcces.CreateParameter<T>(this.Name, this.Value);
            }
            return result;
        }

        object IDataAccessParameter.Value
        {
            get{ return this.Value; }
        }

       
        
    }

    /// <summary>
    /// Class that wraps a DbParameter. The type of the parameter is get with GetType() from the Value property.
    /// If Value is null, DbType.String will be choosen.
    /// </summary>
    public class DataAccessParameter : DataAccessParameter<object>
    {
        public DataAccessParameter(string name, object value, DbType type)
            : base(name, value, type)
        {
        }

        public DataAccessParameter(string name, object value): base(name,value)
        {
        }

        public DataAccessParameter(object value):base(value)
        {

        }

        public DataAccessParameter():base()
        {
        }
    }
}
